package com.cookizz.waterfall;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.DataSetObserver;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Adapter;
import android.widget.ListAdapter;
import com.dobbi.mystaggerddemo.R;
import com.etsy.android.grid.StaggeredGridView;

/**
 * The Waterfall Layout
 * @author dugd
 * @time 2015-02-28
 */
public class Waterfall extends StaggeredGridView {
	
	private Adapter mAdapter;
	private LazyLoadListener mLazyLoadListener;
	private View mFooter;
	private Context mContext;
	
	public Waterfall(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		mContext = context;
		this.setOnScrollListener(new OnScrollListener() {
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				if(scrollState == 0) {
					
					// calculate the content's current scrollY
					View topView = view.getChildAt(0);
					View bottomView = view.getChildAt(view.getChildCount() - 1);
					int containerHeight = view.getHeight();
					int contentScrollY = topView.getTop();
					int contentHeight = bottomView.getBottom() - topView.getTop();
					
					// do something when a user scrolls Waterfall to the very bottom
					if(containerHeight - contentScrollY == contentHeight) {
						if(mFooter != null) {
							if(mFooter instanceof ILazyLoadFooter) { // if a footer impelmented the specified interface, then meet its need
								ILazyLoadFooter hint = (ILazyLoadFooter) mFooter;
								hint.show();
							}
							else { // if not, do something by default
								mFooter.setVisibility(View.VISIBLE);
							}
						}
						if(mLazyLoadListener != null) { // trigger a 'loadReady' callback when scrolled to the very bottom
							mLazyLoadListener.onLazyLoadReady();
						}
					}
				}
			}
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {}
		});
	}
	
	@SuppressLint("InflateParams")
	@Override
	public void setAdapter(ListAdapter adapter) {
		mAdapter = adapter;
		mAdapter.registerDataSetObserver(new DataSetObserver() {
			@Override
			public void onChanged() {
				super.onChanged();
				if(mLazyLoadListener != null) { // trigger a 'loadFinish' callback when adapter's dataset is changed
					mLazyLoadListener.onLazyLoadFinish();
				}
				if(mFooter != null) {
					if(mFooter instanceof ILazyLoadFooter) { // if a footer impelmented the specified interface, then meet its need
						ILazyLoadFooter hint = (ILazyLoadFooter) mFooter;
						hint.hide();
					}
					else { // if not, do something by default
						mFooter.setVisibility(View.INVISIBLE);
					}
				}
			}
		});
		
		// add default footer view
		if(mFooter == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			View footer = inflater.inflate(R.layout.default_footer, null);
			mFooter = footer;
			addFooterView(footer);
		}
		
		super.setAdapter(adapter);
	}
	
	@Override
	public void addFooterView(View v) {
		if(mFooter != null) { // remove the maintaining footer
			super.removeFooterView(mFooter);
			mFooter = null;
		}
		
		// add a new footer
		super.addFooterView(v);
		mFooter = v;
		
		if(v instanceof ILazyLoadFooter) { // if a footer impelmented the specified interface, then meet its need
			ILazyLoadFooter hint = (ILazyLoadFooter) v;
			hint.hide();
		}
		else { // if not, choose default
			mFooter.setVisibility(View.INVISIBLE);
		}
	}
	
	/**
	 * set a dynamic-load listener
	 * @param pullListener
	 */
	public void addLazyLoadListener(LazyLoadListener listener) {
		mLazyLoadListener = listener;
	}
}
